using System.Collections.Generic;
using CxRomos.Core.DataAccess;
using CxRomos.Core.Domain.Entities;
using StructureMap;

namespace CxRomos.Core.Services.Implementation {
    [Pluggable("Default")]
    public class AccountService : IAccountService  {
        private readonly IAccountRepository _accountRepository;
        private readonly IPermissionRepository _permissionRepository;
        private readonly IUserSession _userSession;
        private readonly IRedirector _redirector;
        private readonly IEmail _email;

        public AccountService() {
            _accountRepository = ObjectFactory.GetInstance<IAccountRepository>();
            _permissionRepository = ObjectFactory.GetInstance<IPermissionRepository>();
            _userSession = ObjectFactory.GetInstance<IUserSession>();
            _redirector = ObjectFactory.GetInstance<IRedirector>();
            _email = ObjectFactory.GetInstance<IEmail>();
        }

        public bool UsernameInUse(string username) {
            var account = _accountRepository.GetAccountByUsername(username);

            return account != null;
        }

        public bool EmailInUse(string email) {
            var account = _accountRepository.GetAccountByEmail(email);

            return account != null;
        }

        public string Login(string username, string password) {
            password = password.Encrypt(username);
            Account account = _accountRepository.GetAccountByUsername(username);

            if(account != null) {
                if(account.Password == password) {
                    if(account.EmailVerified) {
                        _userSession.LoggedIn = true;
                        _userSession.Username = username;
                        _userSession.CurrentUser = GetAccountById(account.AccountId);
                        _redirector.GoToHomePage();
                    } else {
                        _email.SendEmailAddressVerificationEmail(account.Username, account.Email);
                        return @"The login information you provided was correct 
                        but your email address has not yet been verified.  
                        We just sent another email verification email to you.  
                        Please follow the instructions in that email.";
                    }
                } else {
                    return "We were unable to log you in with that information!";
                }
            }
            return "We were unable to log you in with that information!";
        }

        public Account GetAccountById(int accountId) {
            Account account = _accountRepository.GetAccountById(accountId);
            List<Permission> permissions = _permissionRepository.GetPermissionsByAccountId(accountId);
            foreach (var permission in permissions) {
                account.AddPermission(permission);
            }

            return account;
        }

        public void Logout() {
            _userSession.LoggedIn = false;
            _userSession.CurrentUser = null;
            _userSession.Username = "";
            _redirector.GoToAccountLoginPage();
        }
    }
}